﻿using System;
using System.Collections;
using System.Text;
using Peekaboo2008.model;
using System.Data;
using Peekaboo2008.util;
using System.Data.SqlClient;


namespace Peekaboo2008.dao
{
    class DonViQuiDoiDAO
    {
        public static DataTable GetAll(){
            return DataUtil.executeStore("sp_DonViQuiDoi_GetAll", null);
        }

        public static Boolean insert(DonViQuiDoiModel model)
        {
            SqlParameter[] param = new SqlParameter[4];
            int i = 0;
            param[i] = new SqlParameter("Don_vi_goc", SqlDbType.NVarChar);
            param[i++].Value = model.DonViGoc;
            param[i] = new SqlParameter("So_luong", SqlDbType.NVarChar);
            param[i++].Value = model.SoLuong;
            param[i] = new SqlParameter("Child_id", SqlDbType.NVarChar);
            param[i++].Value = model.ChildId;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.NVarChar);
            param[i++].Value = model.GhiChu;

            return DataUtil.executeNonStore("sp_DonViQuiDoi_Insert", param);
        }
        public static Boolean update(DonViQuiDoiModel model)
        {
            SqlParameter[] param = new SqlParameter[5];
            int i = 0;
            param[i] = new SqlParameter("ID", SqlDbType.NVarChar);
            param[i++].Value = model.Id;
            param[i] = new SqlParameter("Don_vi_goc", SqlDbType.NVarChar);
            param[i++].Value = model.DonViGoc;
            param[i] = new SqlParameter("So_luong", SqlDbType.NVarChar);
            param[i++].Value = model.SoLuong;
            param[i] = new SqlParameter("Child_id", SqlDbType.NVarChar);
            param[i++].Value = model.ChildId;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.NVarChar);
            param[i++].Value = model.GhiChu;

            return DataUtil.executeNonStore("sp_DonViQuiDoi_Update", param);
        }
        public static DonViQuiDoiModel GetByID(String _ID)
        {
            DonViQuiDoiModel _model = null;
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Id", SqlDbType.Int);
            param[0].Value = _ID;

            DataTable _dt = DataUtil.executeStore("sp_DonViQuiDoi_GetById", param);

            try
            {
                foreach (DataRow dr in _dt.Rows)
                {
                    _model = new DonViQuiDoiModel();
                    _model.Id = dr["ID"].ToString();
                    _model.DonViGoc = dr["Don_vi_goc"].ToString();
                    _model.SoLuong = dr["So_luong"].ToString();
                    _model.ChildId = dr["Child_id"].ToString();
                    _model.GhiChu = dr["Ghi_chu"].ToString();
                    _model.NgayCapNhapCuoi = dr["Ngay_cap_nhat_cuoi"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            return _model;
        }
    }
}
